import subprocess

# this script clean up all tmux sessions with name starting with "G"
# 1. list all tmux sessions starting with "G"
# 2. ask user to confirm deletion
# 3. delete all tmux sessions starting with "G"
def get_g_sessions():
  result = subprocess.run(['tmux', 'list-sessions', '-F', '#S'], capture_output=True, text=True)
  if result.returncode != 0:
    print("No tmux sessions found or tmux not running.")
    return []
  sessions = [s for s in result.stdout.strip().split('\n') if s.startswith('G')]
  return sessions

def main():
  sessions = get_g_sessions()
  if not sessions:
    print("No tmux sessions starting with 'G' found.")
    return
  print("The following tmux sessions will be deleted:")
  for s in sessions:
    print(f"  {s}")
  confirm = input("Are you sure you want to delete these sessions? (y/N): ").strip().lower()
  if confirm != 'y':
    print("Aborted.")
    return
  for s in sessions:
    subprocess.run(['tmux', 'kill-session', '-t', s])
    print(f"Deleted session: {s}")

if __name__ == "__main__":
  main()